

import java.io.*;
import java.util.ArrayList;
import ocsf.server.*;

public class Server extends AbstractServer {

	private mysqlConnection sqlCon;
	//The default port to listen on.
	final public static int DEFAULT_PORT = 5555;


	public Server() 
	  {
	    super(DEFAULT_PORT); 
	    sqlCon = new mysqlConnection();
	  }
	
	protected void serverStarted() {
		System.out.println("Server listening for connections on port "
				+ getPort());
	}

	protected void serverStopped() {
		System.out.println("Server has stopped listening for connections.");
	}
	
	protected void handleMessageFromClient(Object msg, ConnectionToClient client) {
		ArrayList<String> messages=(ArrayList<String>) msg;
		ArrayList<String> res;
		String command = messages.get(0);
		
		if (command.equals("LogIn")) {
			String username = messages.get(1);
			String password = messages.get(2);
			res = sqlCon.LogIn(username, password);
			this.sendToAllClients(res);
		}

		if (command.equals("AddNewUser")) {
			String userID = messages.get(1);
			String username = messages.get(2);
			String password = messages.get(3);
			String type = messages.get(4);
			res = sqlCon.AddNewUser(userID, username, password, type);
			this.sendToAllClients(res);
		}
		
		if (command.equals("ViewUser")) {
			String userID = messages.get(1);
			res = sqlCon.ViewUser(userID);
			this.sendToAllClients(res);
		}
	}
	
	public static void main(String[] args)  {
		try {
			Server sv=new Server();
			sv.listen();
			
		} catch (IOException e) {
			System.out.println("ERROR - Could not listen for clients!");
			e.printStackTrace();
		}
	}
}
